<?php
/**
 * 职位类型管理
*/
apf_require_class("Bll_BLL");
class Bll_Hr_Position extends BLL{
    /**
     * 根据position_ID查询职位信息
     * @param array $ids
     * @return array
     */
    public function get_position_by_ids($ids, $is_deleted = 1){
        $return = array();
        if($ids || is_array($ids)){
            $ids = array_unique($ids);
            $where['position_id IN('.implode(',', $ids).') AND ?'] = 1;
            $where['is_deleted'] = $is_deleted;
            $result = $this->get_position_dao()->find($where);
            if($result){
                foreach($result as $row){
                    $return[$row['position_id']] = $row;
                }
            }
        }

        return $return;
    }
    /**
     * 根据职能ID获取职位列表(function_position)
     * @param unknown_type $depid
     * @return multitype:
     */
    public function get_position_by_function($depid, $is_deleted = 1){
        $return = array();
        if(!empty($depid)){
            $result = $this->get_functionposition_dao()->find(array('function_id' => intval($depid),'is_deleted'=> $is_deleted));
        }
        if($result){
            foreach($result as $row){
                $return[$row['position_id']] = $row;
            }
        }
        return $return;
    }
    /**
     * 根据职能获取，职能下的所有职位类型
     * @param int $function_id
     * @param string $name
     */
    public function get_position_by_name($function_id,$name){
        $data = array();
        if (empty($name) || empty($function_id)) {
            return $data;
        }
        $_where["is_deleted"]=1;
        $_where["position_name LIKE '%{$name}%' AND 1=?"] = 1;
        $tmp_pos_data = $this->get_position_dao()->find_short_assoc($_where, 'position_name DESC',0);
        if (empty($tmp_pos_data)) {
            return $data;
        }
        $pos_ids = array_keys($tmp_pos_data);
        //得到对应的function
        $_p_where["function_id"] = $function_id;
        $_p_where["is_deleted"]  = 1;
        $_p_where["position_id IN (".implode(',', $pos_ids).") AND 1=?"] = 1;
        $pf_data = $this->get_functionposition_dao()->find($_p_where);
        if (empty($pf_data)) {
            return $data;
        }
        foreach($pf_data as $val){
            $keys = array_search($val['position_id'], $pos_ids);
            $data[$keys] = array(
                    'position_id'=>$val['position_id'],
                    'position_name'=>$tmp_pos_data[$val['position_id']]['position_name'],
                    'function_id'=>$val['function_id'],
                    );
        }
        ksort($data);
        return $data;
    }
    /**
     * 新增岗位和职能部门对应关系
     * @param int $function_id
     * @param string $name
     * @return boolean
     */
    public function insert_position($function_id,$name){
        $status = false;
        if (empty($name) || empty($function_id)) {
            return $status;
        }
        $name = trim($name);
        $_where["position_name"] = $name;
        $tmp_pos_data = $this->get_position_dao()->find_row($_where);
        if(!empty($tmp_pos_data)){
            $posid = $tmp_pos_data['position_id'];
        }else{
            $insert_pos = array(
                    'position_name'=>$name,
                    'is_deleted'=>1,
                    'created'=>time()
            );
            $posid = $this->get_position_dao()->insert($insert_pos);
        }
        //再检测关系是否已存在
        $insert = array(
                'function_id'=>$function_id,
                'position_id'=>$posid,
                'is_deleted'=>1
        );
        $res = $this->get_functionposition_dao()->find($insert);
        if(!empty($res)){
            return $posid;
        }
        $insert['created'] = time();

        $this->get_functionposition_dao()->insert($insert);
        return $posid;
    }
    private function get_position_dao(){
        $dao = $this->get_dao('Dao_Hr_Position');
        return $dao;
    }
    private function get_functionposition_dao(){
        $dao = $this->get_dao('Dao_Hr_FunctionPosition');
        return $dao;
    }
}